<?php

namespace app\common\model\xilumarket;

use app\common\model\User;
use think\Db;
use think\Exception;
use think\Log;
use think\Model;
use function fast\array_except;
use function fast\array_get;


class UserAuth extends Model
{
    // 表名
    protected $name = 'xilumarket_user_auth';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $append = [

    ];


    public static function apply($params,$userId){
        $validator = new \app\common\validate\xilumarket\UserAuth();
        if(!$validator->check($params)) {
           throw new Exception($validator->getError());
        }
        $model = new self();
        $auth = $model->where(['user_id'=>$userId])->find();
        if($auth && $auth->check_status==1){
            throw new Exception('您已通过审核');
        }
        $params['user_id'] = $userId;
        if($auth){
            //判断是否需要审核
            $shopinfo = Config::getMyConfig('shopinfo');
            if($shopinfo && $shopinfo['auth_check_switch'] == 0){
                $params['check_status'] = 1;
            }else{
                $params['check_status']=0;
            }

            $auth->allowField(['check_status','user_id','username','idcard_type','idcard_no','idcard_face_image','idcard_back_image'])->save($params);
        }else{
            $model->allowField(true)->save($params);
            $auth = $model;
        }
        return $auth;
    }

    /**
     * 用户是否认证
     * @param $userId
     * @param $recordId
     * @return bool
     */
    public static function isAuth($userId) {
        if(!$userId)
            return false;
        $row = static::where('user_id',$userId)
                ->find();
        return $row;
    }
}
